﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using WebMvc.Areas.Sys.Models;
using WebMvc.Controllers;
using WebMvc.Models;

// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace WebMvc.Areas.Sys.Controllers
{
    [Area("Sys")]
    [Authorize(Roles = "super")]
    public class MenuController : BaseController<Menu>
    {
        public MenuController(EFDbContext context) : base(context)
        {
        }
        
        /// <summary>
        /// 获取功能树JSON数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTreeData()
        {
            var menus = GetEntitysByLambdaAndSort(o => o.Id > 0,OrderLambda,IsASC).ToList();
            List<TreeModel> treeModels = new List<TreeModel>();
            foreach (var menu in menus)
            {
                treeModels.Add(new TreeModel() { Id = menu.Id.ToString(), Text = menu.Name, Parent = menu.ParentId == 0 ? "#" : menu.ParentId.ToString() });
            }
            return Json(treeModels);
        }
        /// <summary>
        /// 获取子级功能列表
        /// </summary>
        /// <returns></returns>
        public IActionResult GetChildrenByParent(int parentId=0, int pageIndex=1)
        {
            var rowCount=0;
            var totalPages = 0;             
            var result = GetPagedEntitysByLambdaAndSort(pageIndex,PageSize, out rowCount, out totalPages, o => o.ParentId == parentId, OrderLambda, IsASC);
            
            return Json(new
            {
                rowCount = rowCount,
                pageCount = totalPages,
                rows = result,
            });
        }

        /// <summary>
        /// 系统登录后，会在session中记录当前登录用户的信息。
        /// 由于一个用户可以拥有多个角色，我们需要做的是，根据当前登录用户的Id，得到其拥有的所有角色信息，
        /// 然后取当前用户所有角色拥有的功能权限求合集，得到的就是当前登录用户所拥有的功能权限信息。
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public IEnumerable<Menu> GetMenusByUserId(int userId)
        {
            var allMenus = GetEntitysByLambdaAndSort(o => o.Id > 0, o => o.SortIndex, true).ToList();
            if (userId == 1)   //超级管理员，此处采用了硬编码,获取所有菜单
                return allMenus;

            //根据用户Id获取用户信息            
            var user = _context.Set<User>().SingleOrDefault(o=>o.Id==userId);                

            //根据用户信息中的roleid获取角色所对应的菜单信息
            //RoleMenuService _rms = new RoleMenuService();
            //var menus = _rms.GetEntitysByLambdaAndSort(o => o.RoleId == user.RoleId, o => o.SortIndex, true);

            //List<int> menuIds = new List<int>();
            RoleMenuController _rms = new RoleMenuController();
            var menuIds = _rms.GetMenuIdsByRoleId(user.RoleId).ToList();
            var menus = allMenus.Where(it => menuIds.Contains(it.Id)).ToList();
            var tempmenus = allMenus.FindAll(it => it.ParentId == 0);//顶级菜单必须有，否则子菜单不显示
            var result = menus.Union(tempmenus).ToList();

            return result;
        }

    }
}
